﻿package ua.mihaylenko.canvas.common 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.display.StageScaleMode;
	import flash.events.ProgressEvent;
	import ua.mihaylenko.canvas.utils.Preloader;
	import ua.mihaylenko.canvas.controller.CanvasController;
	import ua.mihaylenko.canvas.data.DataManager;
	import flash.system.Security;
	import flash.system.System;
	/**
	 * ...
	 * @author Mihayelnko A.L.
	 */
	public class DocumentClass extends MovieClip
	{
		public static var AppStage:Stage;
		public static var ROOT:Sprite;
		public function DocumentClass() 
		{
			stop();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		private function init(event:Event = null):void {
			AppStage = stage;
			ROOT = this;
			Security.allowDomain("*");
			System.useCodePage = true;
			stage.showDefaultContextMenu = false;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			removeEventListener(Event.ADDED_TO_STAGE, init);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
		}
		private function progress(event:ProgressEvent) {
			Preloader.getInstance().progress = Number(event.bytesLoaded / event.bytesTotal);
			if (event.bytesLoaded ==  event.bytesTotal) {
				loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
				DataManager.getInatsnce().addEventListener(Event.COMPLETE, onDataLoaded);
				DataManager.getInatsnce().parseVars(loaderInfo);
			}
		}
		private function onDataLoaded(event:Event) {
			DataManager.getInatsnce().removeEventListener(Event.COMPLETE, onDataLoaded);
			var cController:CanvasController = new CanvasController();
			this.gotoAndStop('start');
		}
	}
	
}